// Always also include CF_bodyjs.js, so that functions like validateData will work.

function CF_ajtable(){

		this.syncVal = function(dbVariable,dbKey,elementID){

			var isValid;
			
			//----
			// Callback object for syncVal operation
			var cb_do_syncval =
				{
				  success: handle_succ_syncval,
				  failure: handle_fail_syncval,
				  argument: { target:"foo", bar:"bar" }
				};		
					
				
			// Callback handler for sucessfull xhr request
			var handle_succ_syncval = function(o){

				if(o.responseText !== undefined){
									 
					result_text = o.responseText;
					el_Item = document.getElementById(o.argument.target);
					el_OldItem = document.getElementById('OV_' + o.argument.target);		

					if(result_text == 'Success'){

						el_OldItem.value = o.argument.newvalue;
						}
					else{
						statusPrint("<b>Error - changes not changed to DB</b>");
						
						if(el_Item.type == 'checkbox'){
							if(o.argument.oldvalue == 0){
								el_Item.checked = false;
							}
							else{
								el_Item.checked = true;
							}
						}
						else{
							el_Item.value = o.argument.oldvalue;
						}
					}
					
			//		elDebug.innerHTML += result_text;

				}
			}

			// callback function for fail on xhr request
			var handle_fail_syncval = function(o){
				el = document.getElementById(o.argument.target);
				el.innerHTML = "Failed data retrieve";
				}
			// -------		
			
			isValid = validateData(dbVariable,dbKey,elementID);
			if(!isValid){
				return;
				}

			var elDebug = document.getElementById('debug_text');

			var valElement = document.getElementById(elementID)

			if(valElement.type == 'checkbox'){
				if(valElement.checked){
					var newvalue = '1';
				}
				else{
					var newvalue = '0';
				}
			}
			else{
				var newvalue = valElement.value;
			}

			var oldValElID = 'OV_' + elementID;
			var oldvalue = document.getElementById(oldValElID).value;
			
			elDebug.innerHTML = "Variable = " + dbVariable + " And key(s) = " + dbKey + " and changeElName = " + elementID + " newvalue= " + newvalue + " FROM: " + oldvalue + 'ELtype= ' + valElement.type;


			params_sreqheader = "rqkeyvalues=" + escape(dbKey) + "&mode=update&var=" + escape(dbVariable) + "&value=" + escape(newvalue) + "&rqkeyvariables=" + escape(this.keyvariables);
			
			var request_s = YAHOO.util.Connect.asyncRequest('POST', this.url_syncval,cb_do_syncval, params_sreqheader); 
			
			cb_do_syncval.argument.target = elementID;
			cb_do_syncval.argument.newvalue = newvalue;
			cb_do_syncval.argument.oldvalue = oldvalue;

		}

		
			

	
} // end CF_ajtable


function statusPrint(msg){
	var elStatus = document.getElementById('status_text');
	elStatus.innerHTML = msg;
}

